program brentry
version 1.0
args lnf s v f alpha1 alpha2 alpha3 gamma1 gamma2 gamma3 
tempvar p1 p2 p3 pi_u1 pi_u2 pi_u3

qui gen double `pi_u1'=`s'*(`v'+`alpha1')-`f'-`gamma1'
qui gen double `pi_u2'=`s'*(`v'+`alpha1'-`alpha2')-`f'-`gamma1'-`gamma2'
qui gen double `pi_u3'=`s'*(`v'+`alpha1'-`alpha2'-`alpha3')-`f'-`gamma1'-`gamma2'-`gamma3'

qui gen double `p1'=normal(-1*`pi_u1')
qui gen double `p2'=normal(-1*`pi_u2')
qui gen double `p3'=normal(-1*`pi_u3')

qui replace `lnf' = ln(`p1') if $ML_y1==0
qui replace `lnf' = ln(`p2'-`p1') if $ML_y1==1
qui replace `lnf' = ln(`p3'-`p2') if $ML_y1==2
qui replace `lnf' = ln(1-`p3') if $ML_y1>=3
end

